package editor;

import java.awt.Container;
import java.awt.Graphics;
import java.awt.geom.AffineTransform;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;

public class Level 
{
	private int width, height;
	
	private String name;
	private String tsLocation;
	private Tileset tileset;
	
	private int[][] layer1;
	private int[][] layer2;
	private int[][] layer3;
	
	public Level( String name, int w, int h, int[][] l1, int[][] l2, int[][] l3, String tileLoc )
	{
		this.name = name;
		
		width = w;
		height = h;
		
		layer1 = l1;
		
		for( int i = 0; i < layer1.length; i++ )
		{
			for( int j = 0; j < layer1[i].length; j++ )
			{
				layer1[i][j] = 1;
			}
		}
		
		layer2 = l2;
		layer3 = l3;

		tsLocation = tileLoc;
		
		tileset = Tileset.load( tsLocation );
	}
	
	public int getWidth()
	{
		return width;
	}
	
	public int getHeight()
	{
		return height;
	}
	
	public Tileset getTileset()
	{
		return tileset;
	}
	
	public boolean isOccupiedTile( int x, int y )
	{
		 return tileset.getTileWithKey( layer2[x][y] ).isOccupied();
	}
	
	public void setTile( int layer, int x, int y, int key )
	{
		if( layer == 1 )
			layer1[x][y] = key;
		else if( layer == 2 )
			layer2[x][y] = key;
		else if( layer == 3 )
			layer3[x][y] = key;
	}
	
	public void render( int layer, Graphics g )
	{
		Tile t;
		for( int x = 0; x < width; x++ )
		{
			for( int y = 0; y < height; y++ )
			{
				if( layer == 1 )
				{
					t = tileset.getTileWithKey( layer1[x][y] );
					if( t != null )
						t.getImage().draw( g, AffineTransform.getTranslateInstance( x * 32, y * 32 ) );
				}
				else if( layer == 2 )
				{
					t = tileset.getTileWithKey( layer2[x][y] );
					if( t != null )
						t.getImage().draw( g, AffineTransform.getTranslateInstance( x * 32, y * 32 ) );
				}
				else if( layer == 3 )
				{
					t = tileset.getTileWithKey( layer3[x][y] );
					if( t != null )
						t.getImage().draw( g, AffineTransform.getTranslateInstance( x * 32, y * 32 ) );
				}
			}
		}
	}
	
	public static Level load( String filename )
	{
		File f = new File( filename );
		
		try 
		{
			FileReader stream = new FileReader( f );
			BufferedReader buf = new BufferedReader( stream );

			String name = buf.readLine();
			
			int width = Integer.valueOf( buf.readLine() );
			int height = Integer.valueOf( buf.readLine() );
			int[][] l1 = new int[width][height];
			
			for( int x = 0; x < width; x++ )
			{
				for( int y = 0; y < height; y++ )
				{
					l1[x][y] = Integer.valueOf( buf.readLine() );
				}
			}
			
			int[][] l2 = new int[width][height];
			
			for( int x = 0; x < width; x++ )
			{
				for( int y = 0; y < height; y++ )
				{
					l2[x][y] = Integer.valueOf( buf.readLine() );
				}
			}
			
			int[][] l3 = new int[width][height];
			
			for( int x = 0; x < width; x++ )
			{
				for( int y = 0; y < height; y++ )
				{
					l3[x][y] = Integer.valueOf( buf.readLine() );
				}
			}
			
			String tileset = buf.readLine();
			
			return new Level( name, width, height, l1, l2, l3, tileset );
			
		} 
		catch ( FileNotFoundException e ) 
		{
			e.printStackTrace();
		} 
		catch ( IOException e ) 
		{
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static Level load( Container frame )
	{
		JFileChooser fc = new JFileChooser();
		fc.setCurrentDirectory( new File( "C:/Documents and Settings/Owner/Desktop/Projects/Level Editor/Levels" ) );
		int returnVal = fc.showOpenDialog( frame );
		
		if( returnVal == JFileChooser.APPROVE_OPTION )
		{
			return load( fc.getSelectedFile().getAbsolutePath() );
		}
			
		return null;
	}
	
	public void save()
	{
		File f = new File( "Levels/" + name + ".txt" );
		
		try 
		{ 
			f.createNewFile();
			BufferedWriter buf = new BufferedWriter( new FileWriter( f.getAbsolutePath() ) );
			
			String s = name + "\n" + width + "\n" + height + "\n";
				
			for( int x = 0; x < width; x++ )
			{
				for( int y = 0; y < height; y++ )
				{
					s += "" + layer1[x][y] + "\n";
				}
			}
			
			for( int x = 0; x < width; x++ )
			{
				for( int y = 0; y < height; y++ )
				{
					s += "" + layer2[x][y] + "\n";
				}
			}
			
			for( int x = 0; x < width; x++ )
			{
				for( int y = 0; y < height; y++ )
				{
					s += "" + layer3[x][y] + "\n";
				}
			}
			
			s += tsLocation;
			
			buf.write( s );
			
			buf.close();
		} 
		catch ( FileNotFoundException err ) 
		{
			err.printStackTrace();
		} 
		catch ( IOException err ) 
		{
			err.printStackTrace();
		}
	}
}
